# !python3      # 使用工具:PyCharm
# 用户登录名:yyds_l
# 项目名称: python爬虫     文件名称:爬取_c7_学习资料免费领
# 创建日期:2022/3/29      创建时间:21:48
# 2022/03/29 21:48

"""ok"""


# 调用
def runPs():

    url = "http://www.mobiletrain.org/page/it/?" \
          "changsha=mopinpaibdtg=pinpaipc=1808164317&jzl_kwd=140506048292&jzl_ctv=52189857105" \
          "&jzl_mtt=1&jzl_adt=cl1&jzl_ch=11&jzl_act=2770118&jzl_cpg=113048642" \
          "&jzl_adp=254830029&jzl_sty=7" \
          "&jzl_dv=1"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
                      " AppleWebKit/537.36 (KHTML, like Gecko)"
                      " Chrome/99.0.4844.84"
                      " Safari/537.36"
    }
    res = urllib.request.Request(url=url, headers=headers)
    html = urllib.request.urlopen(res)
    data = html.read().decode('utf-8')
    tree = etree.HTML(data)
    print("开始获取数据" + "-" * 18, "\n\n")
    h1_data = tree.xpath("//div//h3[@class='tit']/text()")
    h1_span_data = tree.xpath("//div//h3[@class='tit']/span/text()")
    h1_class = tree.xpath("//div//h3[@class='tit']/@class")
    '''c7   container'''
    # 标题 >>>    学习资料免费领     tit
    tit = str(h1_data[4]) + str(h1_span_data[2])
    # print(h1_class[4], tit)
    # 内容 >>>
    # 左边
    c7_top_left = tree.xpath("//div//div[@class='c7-top']/div[1]/@class")[0]
    c7_top_left = str(c7_top_left).split()[0]
    # print(c7_top_left)
    img = tree.xpath("//div//div[@class='c7-top']/div[1]//div//img/@src")
    # print(img)
    p = tree.xpath("//div//div[@class='c7-top']/div[1]//div//p/text()")
    # print(p)
    c7_top_left_mask = tree.xpath("//div[@class='c7-top-left-mask']/a/@href")
    # print(c7_top_left_mask)

    # 右边
    c7_top_right = tree.xpath("//div//div[@class='c7-top']/div[2]/@class")[0]
    c7_top_right = str(c7_top_right).split()[0]
    # print(c7_top_right)
    # 1
    con1 = tree.xpath("//div//div[@class='c7-top']/div[2]/ul/@class")
    # print(con1[0])
    con1_p = tree.xpath("//div//ul[@class='c7-top-con1']/li//p/text()")
    # print(con1_p)
    con1_a = tree.xpath("//div//ul[@class='c7-top-con1']/li//a/text()")
    # print(con1_a)
    con1_a_url = tree.xpath("//div//ul[@class='c7-top-con1']/li//a/@href")
    # print(con1_a_url)
    con2 = tree.xpath("//div//div[@class='c7-top']/div[2]/ul/@class")
    # print(con2[1])
    con2_p = tree.xpath("//div//ul[@class='c7-top-con2']/li//p/text()")
    # print(con2_p)
    con2_a = tree.xpath("//div//ul[@class='c7-top-con2']/li//a/text()")
    # print(con2_a)
    con2_a_url = tree.xpath("//div//ul[@class='c7-top-con2']/li//a/@href")
    # print(con2_a_url)
    url_1 = "http://www.mobiletrain.org/page/it/index.css?v4"
    html1 = requests.get(url=url_1, headers=headers)
    html1 = html1.text
    background_img = re.findall(r"images/c7-(.*?).png", html1)
    # http: // www.mobiletrain.org / page / it / images / c7 - r4.png
    # print(background_img)
    os = "http://www.mobiletrain.org/page/it"
    data_s = []
    background_color = "#ffffff"
    for i in range(0, len(img)):
        # print(p[i], img[i], c7_top_left_mask[i])
        data = {
            'id': i + 1,
            'item_p': p[i],
            'img_url': os + str(img[i]).split(".")[1] + str(img[i]).split(".")[2],
            'a_url': c7_top_left_mask[i]
        }

        data_s.append(data)
    # print(data_s)
    data_lf = []
    left = {
        c7_top_left: data_s,
        # 'background-color': background_color
    }
    data_lf.append(left)
    # print(data_lf)

    # print(con1_p[0], con1_a[0], con1_a_url[0])
    data_right_1 = []
    for s in range(0, len(con1_a_url)):
        # print(con1_p[s], con1_a[s], con1_a_url[s])
        data_r = {
            'id': s + 1,
            'con1-p': con1_p[s],
            "con1-a": con1_a[s],
            'con1_a_url': con1_a_url[s]
        }
        data_right_1.append(data_r)
    # print(data_right_1)
    data_right_2 = []
    for k in range(0, len(con2_a_url)):
        # print(con1_p[s], con1_a[s], con1_a_url[s])
        data_r = {
            'id': k + 1,
            'con2-p': con2_p[k],
            "con2-a": con2_a[k],
            'con2_a_url': con2_a_url[k]
        }
        data_right_2.append(data_r)
    # print(data_right_2)
    background_img_list = []
    for op in range(0, len(background_img)):
        background_img = "http://www.mobiletrain.org/page/it/images/c7-" + background_img[op] + ".png"
        background_img_list.append(background_img)
    data_c7_r = []
    data_ro = {
        'c7-top-con1': data_right_1,
        'c7-top-con2': data_right_2,
        'background_img': background_img_list
    }
    data_c7_r.append(data_ro)
    data_rt = []
    data_c7_r = {
        c7_top_right: data_c7_r
    }
    data_rt.append(data_c7_r)
    # print(data_rt)

    data = {
        "tit": tit,
        'container': [left, data_c7_r]
    }
    time.sleep(1)
    print("成功获取数据：\n", data, "\n\n\n")

    # 保存数据
    fl_name = tit + "数据.json"
    with open(fl_name, "w", encoding='utf-8') as fp:
        json.dump(data, fp, ensure_ascii=False)
    time.sleep(2)
    print("数据下载成功！")


# 运行
if __name__ == '__main__':
    import urllib.request
    from lxml import etree
    import requests
    import time
    import json
    import re

    runPs()
